from django.core.paginator import Paginator

from rest_framework.viewsets import ModelViewSet, ViewSet
from rest_framework.decorators import action
from rest_framework import serializers

from main.utils.util_response import DetailResponse, SuccessResponse
from main.system.models import Menu



class MenuSerrializer(serializers.ModelSerializer):
    class Meta:
        model=Menu
        fields = '__all__'


class MenuViewSet(ModelViewSet):
    authentication_classes = []
    queryset = Menu.objects.all()
    serializer_class = MenuSerrializer

    @action(methods=['post'], detail=False, url_path="list")
    def get_menu_list(self, request):
        page=request.data.get('page')
        limit = request.data.get('limit')
        queryset = self.get_queryset()
        p = Paginator(queryset, limit)
        dataList = p.get_page(page).object_list
        serializer = self.get_serializer(dataList, many=True)
        return SuccessResponse(data=serializer.data, total=p.count, limit=limit, page=page)
